package com.cloud.easy.im.processor;

import com.cloud.easy.im.argeement.Agreement;
import com.cloud.easy.im.event.MessageQoSEventListenerS2C;
import com.cloud.easy.im.event.ServerEventListener;
import com.cloud.easy.im.handler.qos.QoS4SendDaemonS2C;
import com.cloud.easy.im.netty.IMObserver;
import com.cloud.easy.im.utils.GlobalSendHelper;
import com.cloud.easy.im.utils.LocalSendHelper;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 通用逻辑处理流程
 * 后续消息通讯方式可以在这里调整为通过mq的方式发送
 *
 * @author wangjian
 * @version 0.1.0
 * @create 2020年09月16日11:32 上午
 */
public class CommonProcessor {

    private static final Logger logger = LoggerFactory.getLogger(CommonProcessor.class);

    /**
     * 单利内部类
     */
    private static class SingletonHolder {
        private static final CommonProcessor INSTANCE = new CommonProcessor();
    }

    /**
     * 获取单利实例
     *
     * @return
     */
    public static CommonProcessor getInstance() {
        return CommonProcessor.SingletonHolder.INSTANCE;
    }

    /**
     * 客户端 --> 客户端 单聊(推送)
     * @param session
     * @param dataFromClient
     * @param serverEventListener
     */
    public void processC2CMessage(Channel session, Agreement dataFromClient, ServerEventListener serverEventListener) throws InterruptedException {
        GlobalSendHelper.sendDataC2C(session, dataFromClient, serverEventListener);
    }

    /**
     * 客户端 --> 客户端 群聊(拉取)
     *
     * @param dataFromClient
     * @param serverEventListener
     */
    public void processC2CGroupMessage(Agreement dataFromClient, ServerEventListener serverEventListener) throws InterruptedException {
        GlobalSendHelper.sendGroupDataC2C(dataFromClient, serverEventListener);
    }

    /**
     * 处理S2C模式下，由客户端返回的应答包
     * @param agreement
     * @param messageQosEventListener
     * @throws Exception
     */
    public void processACK(final Agreement agreement, MessageQoSEventListenerS2C messageQosEventListener) throws Exception {
        //消息指纹信息
        final String theFingerPrint = agreement.getDataContent();
        if (Agreement.serverMark.equals(agreement.getTo())) {
            //发送给服务端
            logger.debug("[IMCORE-本机QoS！]【QoS机制_S2C】收到发送者" + agreement.getFrom() + "回过来的指纹为" + theFingerPrint + "的应答包.");

            if (messageQosEventListener != null) {
                messageQosEventListener.messagesBeReceived(theFingerPrint);
            }

            QoS4SendDaemonS2C.getInstance().remove(theFingerPrint);
        } else {
            OnlineProcessor.getInstance().printOnline();

            IMObserver sendResultObserver = new IMObserver() {
                @Override
                public void update(boolean sendOK, Object extraObj) {
                    logger.debug("[IMCORE-本机QoS！]【QoS机制_C2C】" + agreement.getFrom() + "发给" + agreement.getTo()
                            + "的指纹为" + theFingerPrint + "的应答包已成功转发？" + sendOK);
                }
            };

            LocalSendHelper.sendData(agreement, sendResultObserver);

        }
    }

}
